package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.OrderCarData;

import java.util.List;

/**
 * @Description:
 * @Author: chuang
 */
public class OrderCheckCarListAdapter extends BaseAdapter {

    private List<OrderCarData.DataBean> mDataBean;

    private Context mContext;

    private LayoutInflater mLayoutInflater;
    private int selectPosition = 0;

    public OrderCheckCarListAdapter(List<OrderCarData.DataBean> dataBean, Context context) {
        mDataBean = dataBean;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderCarData.DataBean> dataBean,int selectPosition) {
        this.mDataBean = dataBean;
        this.selectPosition = selectPosition;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mDataBean.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.order_check_car_item, null);
            holder = new ViewHolder();
            holder.checkImg = (ImageView) convertView.findViewById(R.id.check_img);
            holder.car_img = (ImageView) convertView.findViewById(R.id.car_img);
            holder.price_by_num_tv = (TextView) convertView.findViewById(R.id.price_by_num_tv);
            holder.price_by_day_tv = (TextView) convertView.findViewById(R.id.price_by_day_tv);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }
        OrderCarData.DataBean source = mDataBean.get(position);
        convertView.setTag(R.id.tag_second, source);
        if(source != null){
            holder.price_by_num_tv.setText("按辆收费:" + source.getCharge_info().getCar());
            holder.price_by_day_tv.setText("按时收费:" + source.getCharge_info().getTime());
            Glide.with(mContext).load(source.getImg()).placeholder(R.drawable.car_loading).error(R.drawable.car_loading).into(holder.car_img);
        }
        if(selectPosition == position){
            holder.checkImg.setSelected(true);
        }else{
            holder.checkImg.setSelected(false);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    selectPosition = position;
                    notifyDataSetChanged();
            }
        });
        return convertView;
    }


    public static class ViewHolder {
        public ImageView checkImg;
        public ImageView car_img;
        public TextView price_by_num_tv;
        public TextView price_by_day_tv;
    }

    public int getSelectPosition(){
        return selectPosition;
    }
}

